package org.hqs.department.controller;

import org.hqs.department.common.JsonResult;
import org.hqs.department.pojo.Department;
import org.hqs.department.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("department")
public class DepartmentController {

    @Autowired
    private final DepartmentService departmentService;

    @Autowired
    public DepartmentController(DepartmentService departmentService) {
        this.departmentService = departmentService;
    }

    @GetMapping("/")
    public JsonResult<List<Department>>  getAllDepartments() {
        return  new JsonResult<>(departmentService.getAllDepartments());
    }

    @GetMapping("/{id}")
    public JsonResult<Department> getDepartmentById(@PathVariable Long id) {
        return new JsonResult(departmentService.getDepartmentById(id));
    }

//    @GetMapping("/{id}")
//    public Department getDepartmentById(@PathVariable Long id) {
//        return departmentService.getDepartmentById(id);
//    }

    @PostMapping("/")
    public JsonResult saveDepartment(@RequestBody Department department) {
        int res = departmentService.saveDepartment(department);
        if (res > 0) {
            return new JsonResult("200", "新增成功");
        }
        return new JsonResult("400", "似乎新增失败了");
    }

    @PutMapping("/{id}")
    public JsonResult updateDepartment(@PathVariable Long id, @RequestBody Department department) {
        department.setId(id);
        int res = departmentService.updateDepartment(department);
        if (res > 0) {
            return new JsonResult("200", "更改成功");
        }
        return new JsonResult("400", "似乎更改失败了");
    }

    @DeleteMapping("/{id}")
    public JsonResult deleteDepartment(@PathVariable Long id) {
        int res = departmentService.deleteDepartment(id);
        if (res > 0) {
            return new JsonResult("200", "删除成功");
        }
        return new JsonResult("400", "似乎删除失败了");
    }
}
